package com.tetra.arena.core;

import java.io.IOException;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import com.tetra.arena.listeners.ArenaPlayerListener;
import com.tetra.arena.mysql.DataSource;
import com.tetra.arena.mysql.MySQLc;

public class ArenaSystems extends JavaPlugin {
	Logger log = Logger.getLogger("Minecraft");
	private DataSource database;
	YamlConfiguration fc = new YamlConfiguration();
	ArenaPlayerListener apl;
	CommandHandler CH;

	public void onEnable() {
		setConfDefaults();
		try {
			database = new MySQLc(this);
			CH = new CommandHandler(database);
		} catch (Exception ex) {
			log.info(ex.getMessage());
			log.info("Can't load database");
			this.getServer().getPluginManager().disablePlugin(this);
			return;
		}
		
		apl = new ArenaPlayerListener(database, this);
		this.getServer().getLogger().info("ARENAS ACTIVE");
		Bukkit.getServer().getPluginManager().registerEvents(apl, this);
	}

	public void setConfDefaults() {
		System.out.println("Loading config...");
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
		} catch (Exception e) {
			System.out.println("Creating config...");
		}
		if (!fc.contains("server.ip")) {
			fc.set("server.ip", "Your Databse IP Here");
		}
		if (!fc.contains("server.username")) {
			fc.set("server.username", "Your database username here");
		}
		if (!fc.contains("server.port")) {
			fc.set("server.port", "Your database port here");
		}
		if (!fc.contains("server.password")) {
			fc.set("server.password", "Your Database password here");
		}
		if (!fc.contains("server.database")) {
			fc.set("server.database", "Your databases name here");
		}
		try {
			fc.save("plugins/ArenaSeasons/config.yml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Done loading config!");
	}

	public String getmServer() {
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
			return fc.getString("server.ip", "0.0.0.0");
		} catch (Exception e) {
			setConfDefaults();
		}
		return null;
	}

	public String getPort() {
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
			return fc.getString("server.port", "3306");
		} catch (Exception e) {
			setConfDefaults();
		}
		return null;
	}

	public String getUsrn() {
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
			return fc.getString("server.username", "root");
		} catch (Exception e) {
			setConfDefaults();
		}
		return null;
	}

	public String getPswd() {
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
			return fc.getString("server.password", "admin");
		} catch (Exception e) {
			setConfDefaults();
		}
		return null;
	}

	public String getDBName() {
		try {
			fc.load("plugins/ArenaSeasons/config.yml");
			return fc.getString("server.database", "minecraft");
		} catch (Exception e) {
			setConfDefaults();
		} finally {
		}
		return null;
	}

	public void onDisable() {
		System.out.println("Saving Database");
	}

	public boolean onCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {
		Player player = null;
		if (sender instanceof Player) {
			player = (Player) sender;
		}
		if (cmd.getName().equalsIgnoreCase("arena")) {
			if (player != null) {
				if (args.length > 0) {
					CH.interpret(player, args);
					return true;
				}
			}
		}
		if (cmd.getName().equalsIgnoreCase("whoami")) {
			if (player != null) {
				sender.sendMessage("Your Id is:" + database.getIntValue("players", "team_id", "username", -1, player.getName()));
			}
		}
		return false;
	}
}
